//
//  TTRoomRightView.swift
//  SSCodingOsx
//
//  Created by Toj on 3/16/21.
//  Copyright © 2021 TTProduct. All rights reserved.
//

import Cocoa

private let headWH: CGFloat = 27
private let redWH: CGFloat = 8

class TTRoomRightView: NSView {
    
    private lazy var nicknameLabel: TTLabel = {
        let view = TTLabel()
        view.lineBreakMode = .byTruncatingTail
        view.textColor = NSColor.init(hexColor: "#525C65")
        view.stringValue = "张博伟"
        view.font = NSFont.systemFont(ofSize: 12)
        return view
    }()
    
    private lazy var headImageView: NSImageView = {
        let view = NSImageView()
        view.tt_cornerRadius(headWH * 0.5)
        view.image = NSImage(named: "liveTitleHead")
        return view
    }()
    
    private lazy var infoButton: TTButton = {
        let view = TTButton()
        view.tt_addTarget(self, action: #selector(didTapInfo), for: .leftMouseUp)
        return view
    }()
    
    private lazy var tapView: TTRoomTabView = {
        let view = TTRoomTabView()
        view.tabs = ["去教室", "去编程"]
        return view
    }()
    
    public lazy var redView: NSView = {
        let view = NSView()
        view.isHidden = true
        view.tt_cornerRadius(redWH * 0.5)
        view.tt_backgroundColor = NSColor.init(hexColor: "#FC3D39")
        return view
    }()
    
    private lazy var persistentButton: TTButton = {
        let view = TTButton()
        view.tt_setImage(NSImage(named: "livePersistentNormal"), for: .normal)
        view.tt_setImage(NSImage(named: "livePersistentHover"), for: .hover)
        view.tt_addTarget(self, action: #selector(self.didTapPersistent(sender:)), for: .leftMouseUp)
        return view
    }()
    
    private lazy var refreshButton: TTButton = {
        let view = TTButton()
        view.tt_setImage(NSImage(named: "liveRefrenshNormal"), for: .normal)
        view.tt_setImage(NSImage(named: "liveRefrenshHover"), for: .hover)
        view.tt_setImage(NSImage(named: "liveRefrenshPress"), for: .press)
        return view
    }()
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - override
extension TTRoomRightView {
    
    override func layout() {
        super.layout()
        
        setupMakeLayoutSubviews()
    }
}

// MARK: - 点击事件
extension TTRoomRightView {
    // 点击题版
    @objc private func didTapPersistent(sender: NSButton) {
        redView.isHidden = true
    }
    
    // 点击 头像
    @objc private func didTapInfo(sender: NSButton) {
        print(#function)
    }
}

// MARK: - setup
extension TTRoomRightView {
    
    private func setupMakeAddSubviews() {
        addSubview(nicknameLabel)
        addSubview(headImageView)
        addSubview(infoButton)
        addSubview(tapView)
        addSubview(persistentButton)
        addSubview(redView)
        addSubview(refreshButton)
    }
    
    private func setupMakeLayoutSubviews() {
        headImageView.snp.makeConstraints { (make) in
            make.right.equalTo(snp.right).offset(-77)
            make.centerY.equalTo(self)
            make.width.equalTo(headWH)
            make.height.equalTo(headWH)
        }
       
        nicknameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView.snp.right).offset(5)
            make.centerY.equalTo(headImageView)
            make.width.equalTo(72)
        }
        
        infoButton.snp.makeConstraints { (make) in
            make.left.top.bottom.equalTo(headImageView)
            make.right.equalTo(nicknameLabel)
        }
        
        tapView.snp.makeConstraints { (make) in
            make.right.equalTo(headImageView.snp.left).offset(-30)
            make.centerY.equalTo(headImageView)
            make.width.equalTo(120)
            make.height.equalTo(34)
        }
        
        persistentButton.snp.makeConstraints { (make) in
            make.right.equalTo(tapView.snp.left).offset(-30)
            make.centerY.equalTo(headImageView)
            make.width.equalTo(24)
            make.height.equalTo(27)
        }
        
        redView.snp.makeConstraints { (make) in
            make.right.equalTo(persistentButton.snp.right)
            make.top.equalTo(persistentButton.snp.top)
            make.width.equalTo(redWH)
            make.height.equalTo(redWH)
        }
        
        refreshButton.snp.makeConstraints { (make) in
            make.right.equalTo(persistentButton.snp.left).offset(-30)
            make.centerY.equalTo(headImageView)
            make.width.equalTo(32)
            make.height.equalTo(32)
        }
    }
}
